package com.lecyon.farm.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.lecyon.farm.comment.JwtUtil;
import com.lecyon.farm.common.constant.CommonConstant;
import com.lecyon.farm.util.RedisUtils;
import com.lecyon.farm.util.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Map;

/**
 * 自动填充Handler
 *
 * @author Yao Zheng
 */
@Slf4j
@Component
public class FarmMetaObjectHandler implements MetaObjectHandler {

    private RedisUtils redisUtils;

    @Autowired
    public FarmMetaObjectHandler(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 新增的时候自动填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        LocalDateTime currentTime = LocalDateTime.now(ZoneId.systemDefault());
        this.strictInsertFill(metaObject, "crtTime", LocalDateTime.class, currentTime);
        this.strictInsertFill(metaObject, "updTime", LocalDateTime.class, currentTime);
        this.strictInsertFill(metaObject, "delFlag", String.class, CommonConstant.DEL_FLAG_UNDELETED);
        Long currentUserId = null;
        String currentUserName = null;
        String owner = null;
        String userToken = (String) this.getFieldValByName("userToken", metaObject);
        if (StringHelper.isNotEmpty(userToken)) {
            String systemUserId = JwtUtil.getSysUserId(userToken);
            boolean flag = redisUtils.hasKey(systemUserId);
            if (flag) {
                Map<String, Object> redisMap = redisUtils.hmGet(systemUserId);
                if (MapUtils.isNotEmpty(redisMap)) {
                    currentUserId = (Long) redisMap.get("currentUserId");
                    currentUserName = (String) redisMap.get("currentUserName");
                    owner = (String) redisMap.get("currentOwner");
                }
            } else {
                owner = JwtUtil.getLoginCode(userToken);
            }
        }
        this.strictInsertFill(metaObject, "crtUserId", Long.class, currentUserId);
        this.strictInsertFill(metaObject, "crtName", String.class, currentUserName);
        if (StringHelper.isNotEmpty(owner)) {
            this.strictInsertFill(metaObject, "owner", String.class, owner);
        } else {
            this.strictInsertFill(metaObject, "owner", String.class, CommonConstant.DEFAULT_TENANT_ID);
        }
    }

    /**
     * 修改的时候自动填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        LocalDateTime currentTime = LocalDateTime.now(ZoneId.systemDefault());
        this.strictUpdateFill(metaObject, "updTime", LocalDateTime.class, currentTime);
        Long currentUserId = null;
        String currentUserName = null;
        String userToken = (String) this.getFieldValByName("userToken", metaObject);
        if (StringHelper.isNotEmpty(userToken)) {
            String systemUserId = JwtUtil.getSysUserId(userToken);
            boolean flag = redisUtils.hasKey(systemUserId);
            if (flag) {
                Map<String, Object> redisMap = redisUtils.hmGet(systemUserId);
                if (MapUtils.isNotEmpty(redisMap)) {
                    currentUserId = (Long) redisMap.get("currentUserId");
                    currentUserName = (String) redisMap.get("currentUserName");
                }
            }
        }
        this.strictUpdateFill(metaObject, "updUserId", Long.class, currentUserId);
        this.strictUpdateFill(metaObject, "updName", String.class, currentUserName);
    }


}
